/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import system.DbConnection;
import system.Model;

/**
 *
 * @author asus
 */
public class Profil extends Model {

    public static final String IMAGE_DEFAULT = "upload/not_defined.gif";
    public Integer id;
    public String nama_toko = "default";
    public String path_logo = IMAGE_DEFAULT;
    public String deskripsi = "default";
    public String welcome_msg = "";
    public String alamat = "";
    public String telepon = "";
    public String email = "";
    public Integer diskon = 0;

    public Profil() {
    }
    
    public Profil(int id, String nama_toko, String path_logo, String deskripsi,
            String welcome_msg, String alamat, String telepon, String email, int diskon) {
        this.id = id;
        this.nama_toko = nama_toko;
        this.path_logo = path_logo;
        this.deskripsi = deskripsi;
        this.welcome_msg = welcome_msg;
        this.alamat = alamat;
        this.telepon = telepon;
        this.email = email;
        this.diskon = diskon;
    }

    @Override
    public boolean add() {
        String sql =
                "INSERT INTO `tempe`.`profil`("
                + "`nama_toko`, `path_logo`, `deskripsi`, `welcome_msg`, "
                + "`alamat`, `telepon`, `email`, `diskon`)"
                + "VALUES("
                + "'" + nama_toko + "', '" + path_logo + "', '" + deskripsi + "', '" + welcome_msg + "', '"
                + alamat + "', '" + telepon + "', '" + email + "', " + diskon + ")";
        try {
            return getStatement().execute(sql);
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean update() {
        String sql =
                "UPDATE  `tempe`.`profil` SET  "
                + "`nama_toko` =  '" + nama_toko
                + "', `path_logo` =  '" + path_logo
                + "', `deskripsi` =  '" + deskripsi
                + "', `welcome_msg` =  '" + welcome_msg
                + "', `alamat` =  '" + alamat
                + "', `telepon` =  '" + telepon
                + "', `email` =  '" + email
                + "', `diskon` =  " + diskon
                + " WHERE  `profil`.`id` = " + id;
        try {
            return getStatement().execute(sql);
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public static Profil getCurrentProfil() {
        Profil result = new Profil();

        String sql = "SELECT * FROM `tempe`.`profil` LIMIT 0 , 1";

        try {
            ResultSet rs = DbConnection.getStatement().executeQuery(sql);
            if (rs.next()) {
                int getid = rs.getInt("id");
                String getnama_toko = rs.getString("nama_toko");
                String getpath_logo = rs.getString("path_logo");
                String getdeskripsi = rs.getString("deskripsi");
                String getwelcome_msg = rs.getString("welcome_msg");
                String getalamat = rs.getString("alamat");
                String gettelepon = rs.getString("telepon");
                String getemail = rs.getString("email");
                int getdiskon = rs.getInt("diskon");

                result.id = getid;
                result.nama_toko = getnama_toko;
                result.path_logo = getpath_logo;
                result.deskripsi = getdeskripsi;
                result.welcome_msg = getwelcome_msg;
                result.alamat = getalamat;
                result.telepon = gettelepon;
                result.email = getemail;
                result.diskon = getdiskon;

                return result;
            }
        } catch (Exception e) {
            System.out.println("query execute error");
            e.printStackTrace();
        }

        result.add();

        return result;
    }

    @Override
    public void setAttributes(ResultSet rs) throws SQLException {
    }

    @Override
    public boolean validate() {
        return true;
    }

    @Override
    public void initAttributesLabel() {
    }
}